1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class RoundTests {
30 public static void main(String... args) {
31 int failures = 0;
32
33 failures += testNearFloatHalfCases();
34 failures += testNearDoubleHalfCases();
35
36 if (failures > 0) {
37 System.err.println("Testing {Math, StrictMath}.round incurred "
38 + failures + " failures.");
39 throw new RuntimeException();
40 }
41 }
42
43 private static int testNearDoubleHalfCases() {
44 int failures = 0;
45 double [][] testCases = {
46 {+0x1.fffffffffffffp-2, 0.0},
47 {+0x1.0p-1, 1.0},
48 {+0x1.0000000000001p-1, 1.0},
49
50 {-0x1.fffffffffffffp-2, 0.0},
51 {-0x1.0p-1, 0.0},
52 {-0x1.0000000000001p-1, -1.0},
53 };
54
55 for(double[] testCase : testCases) {
56 failures += testNearHalfCases(testCase[0], (long)testCase[1]);
57 }
58
59 return failures;
60 }
61
62 private static int testNearHalfCases(double input, double expected) {
63 int failures = 0;
64
65 failures += Tests.test("Math.round", input, Math.round(input), expected);
66 failures += Tests.test("StrictMath.round", input, StrictMath.round(input), expected);
67
68 return failures;
69 }
70
71 private static int testNearFloatHalfCases() {
72 int failures = 0;
73 float [][] testCases = {
74 {+0x1.fffffep-2f, 0.0f},
75 {+0x1.0p-1f, 1.0f},
76 {+0x1.000002p-1f, 1.0f},
77
78 {-0x1.fffffep-2f, 0.0f},
79 {-0x1.0p-1f, 0.0f},
80 {-0x1.000002p-1f, -1.0f},
81 };
82
83 for(float[] testCase : testCases) {
84 failures += testNearHalfCases(testCase[0], (int)testCase[1]);
85 }
86
87 return failures;
88 }
89
90 private static int testNearHalfCases(float input, float expected) {
91 int failures = 0;
92
93 failures += Tests.test("Math.round", input, Math.round(input), expected);
94 failures += Tests.test("StrictMath.round", input, StrictMath.round(input), expected);
95
96 return failures;
97 }
98 }